package com.xsl.configuration;

import com.xsl.game_rule.Rule;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//作用是为了让Spring Boot扫描到这个配置类，配置类中的内容会被Spring Boot自动加载
@Configuration
public class ChatConfiguration {
    @Bean//springboot会自动扫描到这个方法，并将返回值注入到Spring容器中
    public ChatClient chatClient(OllamaChatModel ollamaChatModel, ChatMemory chatMemory){
        //创建一个日志通知（目的是增强chatClient的功能）
        SimpleLoggerAdvisor simpleLoggerAdvisor = SimpleLoggerAdvisor.builder().build();
        MessageChatMemoryAdvisor messageChatMemoryAdvisor = MessageChatMemoryAdvisor
                .builder(chatMemory).build();
        return ChatClient.builder(ollamaChatModel)
                .defaultAdvisors(simpleLoggerAdvisor,messageChatMemoryAdvisor)
                .defaultSystem("你是宜宾学院的学生，名字叫宾宾")
                .build();
    }
    @Bean
    public ChatMemory ChatMemory(){
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder().build();
        return chatMemory;
    }

    @Bean
    public ChatClient gameChatClient(OpenAiChatModel model,ChatMemory chatMemory){
        SimpleLoggerAdvisor simpleLoggerAdvisor = SimpleLoggerAdvisor.builder().build();
        MessageChatMemoryAdvisor messageChatMemoryAdvisor = MessageChatMemoryAdvisor
                .builder(chatMemory).build();
        return ChatClient
                .builder(model)
                .defaultSystem(Rule.GAME_RULE)
                .defaultAdvisors(
                        simpleLoggerAdvisor,
                        messageChatMemoryAdvisor
                ).build();
    }
}
